#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "besierspline.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actionnew_triggered()
{
    qDebug()<<"new spline";
    auto bs = new besierspline(this);
    connect(bs, &besierspline::select, this, &MainWindow::spline_select);

    //bs->move(50,50);
}

void MainWindow::spline_select(besierspline * bs)
{
    qDebug()<< "select spline";
    if(selectedSpline)
        selectedSpline->setStyleSheet("");
    qDebug()<<bs->geometry();
    selectedSpline=bs;
    selectedSpline->setStyleSheet("border-width: 1px;border-style: solid;border-color: rgb(255, 170, 0);");
}


void MainWindow::on_actiondelete_triggered()
{
    qDebug()<< "delete spline";
    if(!selectedSpline) {
        QMessageBox::warning(this, "未选择对象", "请选择一个曲线对象");
        return;
    }
    delete selectedSpline;
    selectedSpline=NULL;
}


void MainWindow::on_btn_subdivide_chaikin_clicked()
{
    qDebug()<< "chaikin spline";
    if(!selectedSpline) {
        QMessageBox::warning(this, "未选择对象", "请选择一个曲线对象");
        return;
    }
    int num = ui->numspinbox->getNum();
    if(num<1)return;
    selectedSpline->chaikinSubdivide(num);
}


void MainWindow::on_btn_subdivide_cubic_clicked()
{
    qDebug()<< "cubic spline";
    if(!selectedSpline) {
        QMessageBox::warning(this, "未选择对象", "请选择一个曲线对象");
        return;
    }
    int num = ui->numspinbox->getNum();
    if(num<1)return;
    selectedSpline->cubicSubdivide(num);
}


void MainWindow::on_btn_subdivide_fitting_clicked()
{
    qDebug()<< "fitting spline";
    if(!selectedSpline) {
        QMessageBox::warning(this, "未选择对象", "请选择一个曲线对象");
        return;
    }
    int num = ui->numspinbox->getNum();
    if(num<1)return;
    selectedSpline->fittingSubdivide(num);
}

